package com.jims.master.modules.med.service;

import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jims.master.common.web.Dto;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.ClinicOrdersDao;
import com.jims.master.modules.doctor.dao.HisDiagnosisDao;
import com.jims.master.modules.doctor.dao.PrescriptionApplyDao;
import com.jims.master.modules.med.dao.HisVsMedicalDao;
import com.jims.master.modules.med.dao.OrgVsProvinceDao;
import com.jims.master.modules.med.entity.HisVsMedical;
import com.jims.master.modules.med.utils.MedicalComUtil;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * Created by chenxy on 2017/6/9.
 * 医保接口封装
 */
@Service
@Transactional(readOnly = true)
public class MedicalComService {

    @Autowired
    private OrgVsProvinceService orgVsProvinceService;
    @Autowired
    private HisVsMedicalService hisVsMedicalService;
    @Autowired
    private ClinicOrdersDao clinicOrdersDao;
    @Autowired
    private HisDiagnosisDao  hisDiagnosisDao;
    @Autowired
    private OrgVsProvinceDao orgVsProvinceDao;
    @Autowired
    private HisVsMedicalDao hisVsMedicalDao;
    @Autowired
    private PrescriptionApplyDao prescriptionApplyDao;


    /**
     * 获取目标类
     * @return
     */
    public String getTargetClass(){
        return this.orgVsProvinceService.getTargetClass(UserUtils.getOrgId());
    }

    /**
     * 公共调用接口函数
     * @param functionName  调用接口函数名称
     * @param inParams      输入参数
     * @return
     */
   public Dto medicalMethod(String functionName,String inParams){
       String outParams="";
       Dispatch dispatch=MedicalComUtil.newInstance(this.getTargetClass()).getDispatch();
       if(inParams==null || "".equals(inParams)){
           outParams=Dispatch.call(dispatch,functionName).getString();
       }else{
           outParams=Dispatch.call(dispatch,functionName,new Variant(inParams)).getString();
       }
       return MedicalComUtil.bjybDto(outParams);
    }

    /**
     * 费用分解
     * @param inParams
     * @return
     */
    @Transactional(readOnly = false)
    public Dto divide(String inParams){
        Dto returnVal=new BaseDto();
        Dto dto=medicalMethod("Open", "");
        boolean isSuccess=dto.getAsBoolean("isSuccess");
        if(isSuccess){
            //打开读卡设备成功 --可以调用获取个人信息接口
            Dto getDto =medicalMethod("GetPersonInfo", "");
            if(getDto.getAsBoolean("isSuccess")){
                //读取个人信息成功--可以调用费用分解接口
                List<HisVsMedical> list=hisVsMedicalService.hisVsMedicalList(inParams);
                //组织调用费用分解接口的数据
                String clinicNo=inParams.split(",")[inParams.split(",").length-2];//门诊号
                Dto divideDto=new BaseDto();
                divideDto=divideDto(divideDto,inParams,clinicNo);
                String xml=MedicalComUtil.createXMlByDto(divideDto,"Divide");
                returnVal=medicalMethod("Divide",xml);
                //todo 将上传的数据在本地保存一份
             }else{
                returnVal=getDto;
            }
        }else{
            returnVal=dto;
        }
         return returnVal;
    }

    /**
     * 费用分解 准备输入数据
     * @param dto
     * @param clinicOrdersIds
     * @return
     */
    private Dto divideDto(Dto dto,String clinicOrdersIds,String clinicNo){
        dto=setTradeInfo(dto);
        dto=setRecipearray(dto,clinicOrdersIds,clinicNo);
        return dto;
    }

    /**
     * @Author 陈晓阳
     * @desc 封装交易信息
     * @param dto
     * @return
     */
    private Dto setTradeInfo(Dto dto){
        Map<String,String> tradeInfo=new HashMap<String,String>();
        tradeInfo.put("curetype","11");
        tradeInfo.put("illtype","0");
        tradeInfo.put("operator",UserUtils.getUser().getName());
        dto.put("tradeinfo",tradeInfo);
        return dto;
    }

    /**
     * @Author 陈晓阳
     * @desc 封装申请主表 诊断
     * @param dto
     * @param ordersIds
     * @param clinicNo
     * @return
     */
    private Dto setRecipearray(Dto dto,String ordersIds,String clinicNo){
        //1 诊断
        List<BaseDto> diagnosises=hisDiagnosisDao.findByClinicNo(clinicNo,UserUtils.getUser().getId());
        //2 申请主表
        List<BaseDto> applyInfos=prescriptionApplyDao.findByOrdersId(Arrays.asList(ordersIds.split(",")),UserUtils.getOrgId());
        //3主表信息
//        List<Map<String,Object>> recipearray=new ArrayList<Map<String,Object>>();
        //4明细信息
        List<Dto> feeitemarray=new ArrayList<Dto>();
        for (BaseDto apply:applyInfos){
            Map<String,Object> recipear=new HashMap<String,Object>();
//            recipear.put("recipeno",apply.getAsString("recipeno"));
//            recipear.put("recipedate",apply.getAsString("recipedate"));
            String itemClass=apply.getAsString("itemClass");
            String applyId=apply.getAsString("applyId");//主表申请Id
            // 根据申请主表Id 查询明细信息--拿到 price_id 关联 his_vs_medical
            feeitemarray=this.setFeeItemArray(feeitemarray,applyId,itemClass,apply.getAsString("recipeno"));
//            recipearray.add(recipear);
        }
        // 注意：多条诊断 --多条处方  || 一条处方 一个xml标签
        dto.put("diagnosises",diagnosises);//诊断信息
        dto.put("recipearray",applyInfos);//处方信息
        dto.put("feeitemarray",feeitemarray);//明细信息
        return dto;
    }

    /**@Author 陈晓阳
     * @desc 设置明细信息
     * @param feeitemarray  明细集合
     * @param applyId       申请Id
     * @param itemClass     项目类别
     * @param applyOn       申请序号
     * @return
     */
    private List<Dto> setFeeItemArray(List<Dto> feeitemarray, String applyId,String itemClass,String applyOn) {
        String provinceLabel=orgVsProvinceDao.getProvince(UserUtils.getOrgId());
        // 1 根据applyId 查询明细信息
        if("C".equalsIgnoreCase(itemClass)){
            //检验
            feeitemarray.addAll(hisVsMedicalDao.findLabItems(applyId,provinceLabel,applyOn));
        }else if("D".equalsIgnoreCase(itemClass)){
            //检查
            feeitemarray.addAll(hisVsMedicalDao.findExamItems(applyId,provinceLabel,applyOn));
        }else if("A".equalsIgnoreCase(itemClass)){
            //处方
            feeitemarray.addAll(hisVsMedicalDao.findItemInfos(applyId,provinceLabel,applyOn));
        }
        return feeitemarray;
    }

    /**
     * 查询项目类别
     * @param ordersIds
     * @return
     */
    private List<String> getItemClass(String ordersIds){
        List<String> returnVal=new ArrayList<String>();
        returnVal= clinicOrdersDao.findItemClass(Arrays.asList(ordersIds.split(",")));
        return returnVal;
    }








}
